package com.bawei.zk_01

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.base.utils.mvvm.BaseAcitivity
import com.bawei.zk_01.ViewModel.LoginViewModel
import com.bawei.zk_01.databinding.ActivityLoginBinding
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils

@Route(path = "/app/LoginActivity")

class LoginActivity : BaseAcitivity<ActivityLoginBinding,LoginViewModel>() {
    override fun getViewModel(): Class<LoginViewModel> {
        return LoginViewModel::class.java
    }

    override fun getLayout(): Int {
        return R.layout.activity_login
    }

    override fun initView() {

        //勾选用户协议解锁登录按钮
        binding.bt1.isEnabled = false

        binding.cb2.setOnCheckedChangeListener { buttonView, isChecked ->
            binding.bt1.isEnabled = isChecked
        }

        //记住密码
        val boolean = SPUtils.getInstance().getBoolean("isChecked")
        binding.cb1.isChecked = boolean
        if (boolean) {
            binding.et1.setText(SPUtils.getInstance().getString("account"))
            binding.et2.setText(SPUtils.getInstance().getString("password"))
        }

        binding.bt1.setOnClickListener {
            viewModel.login(binding.et1.text.toString(), binding.et2.text.toString())
        }

        //点击跳转修改密码
        binding.tv1.setOnClickListener {
            ARouter.getInstance().build("/app/ResetActivity").navigation()
        }

    }

    override fun initData() {

        viewModel.loginsuccess.observe(this) {
            if (binding.cb1.isChecked) {
                SPUtils.getInstance().put("account", binding.et1.text.toString())
                SPUtils.getInstance().put("password", binding.et2.text.toString())
                SPUtils.getInstance().put("isChecked", true)
            } else {
                SPUtils.getInstance().put("isChecked", false)
            }
            if (it.code == 200) {
                SPUtils.getInstance().put("token", it.data.token)
                ToastUtils.showLong("登录成功")
            }
        }

        viewModel.loginfail.observe(this) {
            ToastUtils.showLong(it)
        }

    }

}